<?php

namespace App\Http\Controllers;

use App\Models\Illegal;
use Illuminate\Http\Request;

class IllegalController extends Controller{

    /**
     * 敏感词
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(){
        $data = Illegal::get();
        return $this->returnJson(0,'',$data);
    }

    /**
     * 敏感词新增
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request){
        try {
            $data = $request->all();
            //重复
            $info = Illegal::where('word',$data['word'])->first();
            if($info){
                return $this->returnJson(1,'哟嚯，该敏感词已经存在');
            }
            Illegal::create($data);
            return $this->returnJson(0);
        } catch (\Exception $e) {
            return $this->returnJson(1, $e->getMessage());
        }

    }

    /**
     * 敏感词删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destory(Request $request){
        try {
            $id = $request->get('id');
            Illegal::destroy($id);
            return $this->returnJson(0);
        } catch (\Exception $e) {
            return $this->returnJson(1, $e->getMessage());
        }

    }

}